﻿using System;
using System.Linq;
using System.Threading.Tasks;

namespace DistTest
{
    public static class Controller
    {
        public static async Task SurveyAsync(ILogging logging)
        {
            var deviceIds = Enumerable.Range(1, 30);
            using (var device = new SerialPortDevice.Port.Dist.DistPort("COM1"))
            {
                foreach (var deviceId in deviceIds)
                {
                    try
                    {
                        var result = await Task.Run(() => device.SurveyDevice(deviceId));
                        await logging.InfoAsync($"{result.DeviceId.ToString("D2")} {result.Distance}");
                    }
                    catch (Exception ex)
                    {
                        await logging.ErrorAsync(ex.Message);
                    }
                }
            }
        }
    }
}
